<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="4" :xs="24">
        <div class="app-container-tree-border">
          <el-input v-model="filterText" placeholder="请输入试验类别" clearable size="small" prefix-icon="el-icon-search" />
          <el-tree
            class="input-tree"
            :data="options"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            highlight-current
            @node-click="handleNodeClick"
          >
            <div slot-scope="{ node, data }" class="custom-tree-node" :title="node.label" v-text="node.label"></div>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <div class="app-container-table-border">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="0px" class="top-left">
            <el-form-item label prop="ypNum">
              <el-input v-model="queryParams.ypNum" placeholder="请输入样品编号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              <el-button type="warning" plain icon="el-icon-download" @click="handleExport">导出</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb5 top-right-btn small-line-height" v-if="false">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['yp:bsYp:add']">新增</el-button>
            </el-col>
          </el-row>

          <el-table border stripe v-loading="loading" :data="bsYpList">
            <el-table-column type="index" fixed label="序号" align="center" prop="ypId" width="60" />
            <el-table-column label="样品编号" fixed align="center" prop="ypNum" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="所属试验室" align="center" prop="deptName" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="工程部位" align="center" prop="gcbw" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="分部分项名称" align="center" prop="fbfxmc" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="样品名称" align="center" prop="yangpmc" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="样品状态" align="center" prop="yangpzt" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="样品数量" align="center" prop="yangpsl" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="代表数量" align="center" prop="daibsl" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="样品日期" align="center" prop="ypDate" width="160" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.ypDate, "{y}-{m}-{d}") }}</span>
              </template>
            </el-table-column>
            <el-table-column label="取样人" align="center" prop="ypPerson" width="120" :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" v-if="false">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['yp:bsYp:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['yp:bsYp:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
      </el-col>
    </el-row>

    <!-- 添加或修改样品对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="800px" append-to-body v-if="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="所属试验">
          <div style="font-weight: bolder; font-size: 16px">{{ this.currentNode.label }}</div>
        </el-form-item>
        <el-form-item label="样品编号" prop="ypNum">
          <el-input v-model="form.ypNum" placeholder="请输入样品编号" />
        </el-form-item>
        <el-form-item label="样品分类" prop="ypClass">
          <el-input v-model="form.ypClass" placeholder="请输入样品分类" />
        </el-form-item>
        <el-form-item label="样品描述" prop="ypDescribe">
          <el-input v-model="form.ypDescribe" placeholder="请输入样品描述" />
        </el-form-item>
        <el-form-item label="工程部位" prop="gcbw">
          <el-input v-model="form.gcbw" placeholder="请输入工程部位" />
        </el-form-item>
        <el-form-item label="分部分项名称" prop="fbfxmc">
          <el-input v-model="form.fbfxmc" placeholder="请输入分部分项名称" />
        </el-form-item>
        <el-form-item label="样品状态" prop="ypStatus">
          <el-input v-model="form.ypStatus" placeholder="请输入样品状态" />
        </el-form-item>
        <el-form-item label="取样日期" prop="ypDate">
          <el-date-picker clearable style="width: 100%" v-model="form.ypDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择取样日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="取样人" prop="ypPerson">
          <el-input v-model="form.ypPerson" placeholder="请输入取样人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="small" @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listBsYp, getBsYp, delBsYp, addBsYp, updateBsYp } from "@/api/yp/bsYp";
import { syTpyeTree } from "@/api/sycs/biQualification";
import {mapGetters} from "vuex";
// import {
//   syTpyeTree,
// } from "@/api/sycs/bsJudgeBase";

export default {
  name: "BsYp",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 样品表格数据
      bsYpList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ypNum: undefined,
        ypClass: undefined,
        tzId: undefined,
        ypDescribe: undefined,
        gcbw: undefined,
        fbfxmc: undefined,
        ypDate: undefined,
        ypPerson: undefined,
        ypStatus: undefined,
        deptId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ypNum: [{ required: true, message: "样品编号不能为空", trigger: "blur" }],
        ypClass: [{ required: true, message: "样品分类不能为空", trigger: "blur" }],
        tzId: [{ required: true, message: "原材料进场台账id可以没有不能为空", trigger: "blur" }],
        ypDescribe: [{ required: true, message: "样品描述不能为空", trigger: "blur" }],
        gcbw: [{ required: true, message: "工程部位不能为空", trigger: "blur" }],
        fbfxmc: [{ required: true, message: "分部分项名称不能为空", trigger: "blur" }],
        ypDate: [{ required: true, message: "样品日期不能为空", trigger: "blur" }],
        ypPerson: [{ required: true, message: "取样人不能为空", trigger: "blur" }],
        ypStatus: [{ required: true, message: "样品状态不能为空", trigger: "change" }]
      },
      filterText: undefined,
      options: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 当前树节点
      currentNode: {}
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
    deptId (newValue, oldValue) {
      this.queryParams.deptId = newValue;
      this.getList();
    }
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["deptId", "userDeptId"])
  },
  created() {
    this.queryParams.deptId = this.$store.state.dept.deptId;
    this.getDeptTree();
    this.getList();
  },
  methods: {
    /** 查询样品列表 */
    getList() {
      this.loading = true;
      listBsYp(this.queryParams).then((response) => {
        this.bsYpList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询树结构 */
    getDeptTree() {
      const params = {
        innerFlag: 1
      };
      syTpyeTree(params).then((response) => {
        this.options = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ypId: undefined,
        ypNum: undefined,
        ypClass: undefined,
        tzId: undefined,
        ypDescribe: undefined,
        gcbw: undefined,
        fbfxmc: undefined,
        ypDate: undefined,
        ypPerson: undefined,
        ypStatus: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.$refs.tree.setCurrentKey(null);
      this.queryParams.syType = null;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      if (!this.currentNode.id) {
        this.$alert("", "请选择试验类型", {
          confirmButtonText: "确定",
          type: "warning",
          center: true
        });
        return;
      }
      this.reset();
      this.open = true;
      this.title = "添加样品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const ypId = row.ypId || this.ids;
      getBsYp(ypId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改样品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (!this.currentNode.id) {
        this.$alert("", "请选择试验类型", {
          confirmButtonText: "确定",
          type: "warning",
          center: true
        });
        return;
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form = {
            ...this.form,
            syType: this.currentNode.id
          };
          this.buttonLoading = true;
          if (this.form.ypId != null) {
            updateBsYp(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addBsYp(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ypIds = row.ypId || this.ids;
      this.$modal
        .confirm('是否确认删除样品编号为"' + ypIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delBsYp(ypIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.$set(this.currentNode, "id", data.id);
      this.$set(this.currentNode, "label", data.label);
      this.queryParams.syType = data.id;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "yp/bsYp/export",
        {
          ...this.queryParams
        },
        `bsYp${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
<style lang="scss" scoped>
@import "~@/assets/styles/demo.scss";
</style>
